import React from 'react';
import menuRoute from 'routes/menuRoute';
import contractService from 'containers/approve/contract/contract.service';
import { Form, Affix, Button, Row, Col, message } from 'antd';
import ContractDetailCommon from 'containers/approve/contract/contract-detail-common';
import ApproveBar from 'components/template/approve-bar';
import { messages } from 'share/common';

class ContractDetail extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      passLoading: false,
      rejectLoading: false,
      isConfirm: this.props.params, //合同审批是否通过
      contract: menuRoute.getRouteItem('approve-contract', 'key'),    //合同
    };
  }

  //获取合同状态
  getStatus = (params) => {
    this.setState({ isConfirm: (params === 1004 || params === 6001 || params === 6002 || params === 6003) });
  };

  //审批通过
  handleApprovePass = (values) => {
    this.setState({ passLoading: true });
    contractService.contractApprovePass(this.props.params.id, values || '')
      .then(res => {
        if (res.status === 200) {
          this.setState({ passLoading: false });
          message.success(messages('common.operate.success'/*操作成功*/));
          this.goBack();
        }
      })
      .catch(e => {
        this.setState({ passLoading: false });
        message.error(`${messages('common.operate.filed'/*操作失败*/)}，${e.response.data.message}`);
      });
  };

  //审批驳回
  handleApproveReject = (values) => {
    this.setState({ rejectLoading: true });
    contractService.contractApproveReject(this.props.params.id, values)
      .then(res => {
        if (res.status === 200) {
          this.setState({ rejectLoading: false });
          message.success(messages('common.operate.success'/*操作成功*/));
          this.goBack();
        }
      })
      .catch(e => {
        this.setState({ rejectLoading: false });
        message.error(`${messages('common.operate.filed'/*操作失败*/)}，${e.response.data.message}`);
      });
  };

  //返回
  goBack = () => {
    if (this.state.isConfirm) {
      this.context.router.push(`${this.state.contract.url}?approved=true`);
    } else {
      this.context.router.push(this.state.contract.url);
    }
  };

  render() {
    const { isConfirm, passLoading, rejectLoading } = this.state;
    return (
      <div className="contract-detail background-transparent" style={{ marginBottom: 50 }}>
        <ContractDetailCommon id={this.props.params.id}
                              isApprovePage={true}
                              getContractStatus={this.getStatus}/>
        {!isConfirm && (
          <Affix offsetBottom={0} className="bottom-bar bottom-bar-approve">
                <ApproveBar
                  passLoading={passLoading}
                  style={{ paddingLeft: 20 }}
                  backUrl={this.state.contract.url}
                  rejectLoading={rejectLoading}
                  handleApprovePass={this.handleApprovePass}
                  handleApproveReject={this.handleApproveReject}/>
          </Affix>
        )}
        {isConfirm &&
        <Affix offsetBottom={0} className="bottom-bar">
          <Button style={{ marginLeft: 33 }}
                  onClick={this.goBack}
                  className="back-btn">{messages('common.back'/*返回*/)}
          </Button>
        </Affix>}
      </div>
    );
  }
}

ContractDetail.contextTypes = {
  router: React.PropTypes.object
};

const wrappedContractDetail = Form.create()(ContractDetail);

export default wrappedContractDetail;

